package com.wsjc.app.timer;

import java.io.File;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.google.common.collect.Maps;
import com.wsjc.common.core.constants.CommonConst;
import com.wsjc.common.core.utils.DateUtils;
import com.wsjc.manager.entity.WjHospital;
import com.wsjc.manager.entity.WjSterilization;
import com.wsjc.manager.entity.WjUser;
import com.wsjc.manager.service.rpc.data.WjSynDataService;
import com.wsjc.manager.service.rpc.map.WjMapService;

@Component
public class DbSynTimerTask {

	private Logger logger = LoggerFactory.getLogger(DbSynTimerTask.class);
	
	@Autowired
	private WjSynDataService wjSynDataServiceImpl ; 
	
	@Autowired
	private WjMapService wjMapServiceImpl ;
	
	
	/**
	 * 同步user数据
	 */
//	@Scheduled(cron="0 */1 * * * ?")
//	@Scheduled(cron = "0/30 * * * * ? ")
	public void synUser() {
		
		try {
			logger.info("定时任务启动 : 同步新老数据库User表数据.....");
			// 启示时间
			long starTime = System.currentTimeMillis();

			Map<String, Object> params = Maps.newHashMap();
			params.put("startTime", DateUtils.addDay(new Date(), -365 * 3) );
			params.put("endTime", new Date());
			
			logger.info("params :" + params.toString());
			
			List<WjUser> users = wjSynDataServiceImpl.queryOldTableUsers(params);
			
			logger.info("old table user size :" + users.size() );
			
			logger.info("old table user[0] info :" + users.get(0).toString() );
			
			wjSynDataServiceImpl.synUser(users);
			
			long Time = System.currentTimeMillis() - starTime;
			System.out.println("一共耗时 ：" + (double) Time / 1000 + " 秒");
		} catch (Exception e) {
			logger.error("同步user数据失败|info:" , e);
		}
		

	}

	public static void main(String[] args) {
	}
	
	
//	@Scheduled(cron="0 */1 * * * ?")
	@Scheduled(cron = "0/50 * * * * ? ")
	public void synHospital() {
		
		try {
			logger.info("定时任务启动 : 同步新老数据库Hospital表数据.....");
			// 启示时间
			long starTime = System.currentTimeMillis();

			Map<String, Object> params = Maps.newHashMap();
			params.put("startTime", DateUtils.addDay(new Date(), -365 * 3) );
			params.put("endTime", new Date());
			
			logger.info("params :" + params.toString());
			
			List<WjHospital> hospitals = wjSynDataServiceImpl.queryOldTableHospitals(params);
			
			logger.info("old table hospital size :" + hospitals.size() );
			logger.info("old table hospital[0] info :" + hospitals.get(0).toString() );
			
			wjSynDataServiceImpl.synHospital(hospitals);
			
			//初始化redis-map
			for(WjHospital wjHospital: hospitals){
				wjMapServiceImpl.initMap("0", wjHospital.getId(), "00S", wjHospital.getAreaId()); 
			}
			
			long Time = System.currentTimeMillis() - starTime;
			logger.info("同步hospital数据 Successful|一共耗时:{}秒" , (double) Time / 1000);
		} catch (Exception e) {
			logger.error("同步hospital数据失败|info:" , e);
		}
		

	}

//	@Scheduled(cron = "0/30 * * * * ? ")
//	@Scheduled(cron="0 */18 * * * ?")
	public void saveSterilization() {
		
		try {
			logger.info("定时任务启动 : 处理旧的灭菌数据并将其保存到本地文件中.....");
			// 启示时间
			long starTime = System.currentTimeMillis();
			
			Map<String, Object> params = Maps.newHashMap();
			/*params.put("startTime", DateUtils.addDay(new Date(), -365 * 3) );
			params.put("endTime", new Date());*/
			
			logger.info("params :" + params.toString());
			
			List<WjSterilization> sterilizations = wjSynDataServiceImpl.queryOldTableSterilization(params) ;
			
			logger.info("old table sterilizations size :" + sterilizations.size() );
			logger.info("old table sterilizations[0] info :" + sterilizations.get(0).toString() );
			
			Thread.sleep(3000);
			wjSynDataServiceImpl.saveSterilizationToLocal(sterilizations);
			
			long Time = System.currentTimeMillis() - starTime;
			logger.info("Sterilization 保存Successful|一共耗时:{}秒" , (double) Time / 1000);
		} catch (Exception e) {
			logger.error("Sterilization 保存 失败|error:" , e);
		}
		
		
	}
	
	
	// @Scheduled(cron = "0/50 * * * * ? ")
//	@Scheduled(cron = "0 */2 * * * ?")
	public void synSterilization() {

		try {
			logger.info("定时任务启动 : 同步新老数据库Sterilization表数据.....");
			// 启示时间
			long starTime = System.currentTimeMillis();

			// 再读取对象
			File file = new File(CommonConst.LOCAL_FILE_PATH_DOWN_OSS_FILE);
			FileInputStream fis = new FileInputStream(file);
			ObjectInputStream objIP = new ObjectInputStream(fis);
			// 读取对象数据，需要将对象流强制转换为 要写入对象的类型
			List<WjSterilization> sterilizations = (List<WjSterilization>) objIP.readObject();

			logger.info("new sterilizations is size{} :", sterilizations.size());

			Thread.sleep(3000);
			
			wjSynDataServiceImpl.synSterilization(sterilizations);

			long Time = System.currentTimeMillis() - starTime;
			logger.info("同步Sterilization数据到db: Successful|一共耗时:{}秒", (double) Time / 1000);
			objIP.close();
		} catch (Exception e) {
			logger.error("同步Sterilization数据到db: 失败|info:", e);
		}

	}
	
	
	
	// @Scheduled(cron = "0/50 * * * * ? ")
//	@Scheduled(cron = "0 */2 * * * ?")
	public void downOssFile() {

		try {
			logger.info("定时任务启动 : 从 oss下载文件定时任务启动.....");
			// 启示时间
			long starTime = System.currentTimeMillis();

			// 再读取对象
			File file = new File(CommonConst.LOCAL_FILE_PATH_DOWN_OSS_FILE);
			FileInputStream fis = new FileInputStream(file);
			ObjectInputStream objIP = new ObjectInputStream(fis);
			// 读取对象数据，需要将对象流强制转换为 要写入对象的类型
			Map<String, String> paths = (Map<String, String>) objIP.readObject();

			logger.info("down oss files is size{} :" , paths.size() * 2);

			Thread.sleep(3000);
			
			wjSynDataServiceImpl.downOssFile(paths);

			long Time = System.currentTimeMillis() - starTime;
			logger.info("下载oss文件到本地 Successful|一共耗时:{}秒", (double) Time / 1000);
		} catch (Exception e) {
			logger.error("下载oss文件到本地 失败|error:", e);
		}

	}
	
	

}
